package com.edu.dao.impl;

import com.edu.dao.AddressesDetailDao;
import com.edu.entity.Address;
import com.edu.entity.AddressEntity;
import com.edu.utils.DatabaseManager;

import java.sql.*;
import java.util.List;

public class AddressesDetailDaoImpl implements AddressesDetailDao {

    private Connection connection;
    public AddressesDetailDaoImpl(Connection connection){
        this.connection = connection;
    }

    @Override
    public Integer addAddressDetail(AddressEntity address) {
        String sql = "INSERT INTO addressdetail(phone,province,city,district,detail_address, postcode) VALUES(?,?,?,?,?,?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setString(1,address.getPhone());
            preparedStatement.setString(2,address.getProvince());
            preparedStatement.setString(3,address.getCity());
            preparedStatement.setString(4,address.getDistrict());
            preparedStatement.setString(5,address.getDetailAddress());
            preparedStatement.setString(6, "123");
            int affectedRows = preparedStatement.executeUpdate();
            Integer generatedKey = null;
            if (affectedRows > 0) {
                try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
                    if (generatedKeys.next()) {
                        generatedKey = generatedKeys.getInt(1);
                    } else {
                        throw new SQLException("Creating addressdetail failed, no ID obtained.");
                    }
                }
            }
            return generatedKey;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public AddressEntity getAddressDetailByAddressId(Integer detailAddressId) {
        String sql = "SELECT * FROM addressdetail WHERE address_detail_id = ?";

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, detailAddressId);
            ResultSet resultSet = preparedStatement.executeQuery();

            AddressEntity addressEntity = new AddressEntity();
            while (resultSet.next()){
                addressEntity.setPhone(resultSet.getString("phone"));
                addressEntity.setProvince(resultSet.getString("province"));
                addressEntity.setCity(resultSet.getString("city"));
                addressEntity.setDistrict(resultSet.getString("district"));
                addressEntity.setDetailAddress(resultSet.getString("detail_address"));
                //addressEntity.setPostcode(resultSet.getInt("postcode"));
            }
            return addressEntity;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int deleteById(Integer addressDetailId) {
        String sql = "delete from addressdetail where address_detail_id = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, addressDetailId);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updatePhone(String phone, Integer addressDetailId) {
        String sql = "update addressdetail set phone = ? where address_detail_id = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, phone);
            preparedStatement.setInt(2, addressDetailId);
            return preparedStatement.executeUpdate();
        }catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
    }
}
